package com.newworld.dmsapp2.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.HistorySearchActivity;
import com.newworld.dmsapp2.activity.HouseManageListActivity;
import com.newworld.dmsapp2.activity.MORE;
import com.newworld.dmsapp2.activity.ToDoTaskListActivity;
import com.newworld.dmsapp2.activity.serviceapply.WriteOutdoorPaperActivity;
import com.newworld.dmsapp2.activity.serviceapply.WritePaperActivity;
import com.newworld.dmsapp2.bean.Function;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.fragment.adapter.HomeMenuItemAdapter;
import com.newworld.dmsapp2.fragment.bean.MenuItem;
import com.newworld.dmsapp2.ui.view.PublicNoticeView;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.SingleSample;

public class MainHomeFragmentPieceNext extends Fragment implements ActionBar.TabListener{
    private ViewPager mPager;
    private ImageView ivBottomLine;
    private TextView tvTabNew, tvTabHot;
	ViewPager mpager;
	ActionBar actionBar;
	private LinearLayout blayout;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	public final static int num = 2 ; 
	Fragment home1;
	Fragment home2;
	private ArrayList<Fragment> fragmentsList;
	
	private GridView home_grid;
	@SuppressWarnings("unused")
	private PublicNoticeView noticeTv;
	Context context;
	private Bundle bundle = new Bundle();
	private Intent intent;
	private User user;
	private List<Function> functions;
	final List<MenuItem> menus = new ArrayList<MenuItem>();
	private List<String> functionCodeList = new ArrayList<String>();
	private List<String> functionCodeListOrder = new ArrayList<String>();//排好序的functioncode列表！
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main_home_piece_next, container,
				false);
		findView(view);
		init();
		return view;
	}
	private void findView(View v) {
		home_grid = (GridView) v.findViewById(R.id.home_grid_next);
		/*noticeTv = (PublicNoticeView) v
				.findViewById(R.id.mainfragment_notice_tv);*/
	}
	
	private void init() {

		user = SingleSample.getInstance(getActivity()).getUser();
		functions = user.getFunctions();
		Log.e("functions", functions.toString());
		menus.clear();
		for (Function function : functions) {
			functionCodeList.add(function.getFunctionCode());
		}
			if (functionCodeList.contains(Config.FUNCTION_REPAIRALLY)) {
				functionCodeListOrder.add(Config.FUNCTION_REPAIRALLY);
			}

			if (functionCodeList.contains(Config.FUNCTION_REPAIRPROGRESS)) {
				functionCodeListOrder.add(Config.FUNCTION_REPAIRPROGRESS);
			}
			if (functionCodeList.contains(Config.FUNCTION_HOUSEMANAGE)) {
				functionCodeListOrder.add(Config.FUNCTION_HOUSEMANAGE);
			}
			if (functionCodeList.contains(Config.FUNCTION_REPAIRTASK)) {
				functionCodeListOrder.add(Config.FUNCTION_REPAIRTASK);
			}
			if (functionCodeList.contains(Config.FUNCTION_REPAIRDISTRIBUTE)) {
				functionCodeListOrder.add(Config.FUNCTION_REPAIRDISTRIBUTE);
			}
			if (functionCodeList.contains(Config.FUNCTION_RESSIGNMENT)) {
				functionCodeListOrder.add(Config.FUNCTION_RESSIGNMENT);
			}
			if (functionCodeList.contains(Config.FUNCTION_MANAGERDISPATCH)) {
				functionCodeListOrder.add(Config.FUNCTION_MANAGERDISPATCH);
			}
			if (functionCodeList.contains(Config.FUNCTION_MANAGERREDISPATCH)) {
				functionCodeListOrder.add(Config.FUNCTION_MANAGERREDISPATCH);
			}
			if (functionCodeList.contains(Config.FUNCTION_OUTTIMEREPAIR)) {
				functionCodeListOrder.add(Config.FUNCTION_OUTTIMEREPAIR);
			}

			if (functionCodeList.contains(Config.FUNCTION_RETURNVISIT)) {
				functionCodeListOrder.add(Config.FUNCTION_RETURNVISIT);
			}
			
			/*if (functionCodeList.contains(Config.FUNCTION_OUTDOORREPAIR)) {
				functionCodeListOrder.add(Config.FUNCTION_OUTDOORREPAIR);
			}*/

			if (functionCodeList.contains(Config.FUNCTION_HISTORYQUERY)) {
				functionCodeListOrder.add(Config.FUNCTION_HISTORYQUERY);
			}
			if (functionCodeList.contains(Config.FUNCTION_GET_WORKER_HISTORYLIST)) {
				functionCodeListOrder.add(Config.FUNCTION_GET_WORKER_HISTORYLIST);
			}
			if (functionCodeList.contains(Config.FUNCTION_PMREVISIT)) {
				functionCodeListOrder.add(Config.FUNCTION_PMREVISIT);
			}
			if (functionCodeList.contains(Config.FUNCTION_OUTDOORREPAIR)) {
				functionCodeListOrder.add(Config.FUNCTION_OUTDOORREPAIR);
			}
			if (functionCodeList.contains(Config.FUNCTION_MYAPPLY)) {
				functionCodeListOrder.add(Config.FUNCTION_MYAPPLY);
			}
			if (functionCodeList.contains(Config.FUNCTION_OUTTIMEREPAIROFDEPART)) {
				functionCodeListOrder.add(Config.FUNCTION_OUTTIMEREPAIROFDEPART);
			}
			System.out.println("functionCodeListOrder---22:"+functionCodeListOrder.toString());
			System.out.println("functionCodeListOrder---22:"+functionCodeListOrder.size());
		judgePageShow();

		context = this.getActivity();

		int margin = (int) (getResources().getDisplayMetrics().density * 14 * 13 / 9);
		HomeMenuItemAdapter adapter = new HomeMenuItemAdapter(getActivity(),
				menus, margin);
		home_grid.setAdapter(adapter);
		home_grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub

				if(null!= menus.get(position).menuFunctionCode && !menus.get(position).menuFunctionCode.equals("")){
					intentActivity(menus.get(position).menuFunctionCode);
				}else{
					Intent intent = new Intent(
							context,MORE.class);
					startActivity(intent);
				}
			}

		});
	}
	//判断数目大于5即生成本fragment
	private void judgePageShow(){
		int funclist_num = functionCodeListOrder.size();
		if(funclist_num>5){
			for(int i=6 ; i<funclist_num ;i++){
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_REPAIRALLY)) {
						menus.add(new MenuItem(R.drawable.grid_repairapplay,
								Config.GRID_REPAIR_APPLY, Config.FUNCTION_REPAIRALLY));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_REPAIRPROGRESS)) {
						menus.add(new MenuItem(R.drawable.grid_tobedo,
								Config.GRID_DOING_STAGE, Config.FUNCTION_REPAIRPROGRESS));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_HOUSEMANAGE)) {
						menus.add(new MenuItem(R.drawable.grid_manyhouse,
								Config.GRID_HOUSE_MANAGEMENT, Config.FUNCTION_HOUSEMANAGE));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_REPAIRTASK)) {
						menus.add(new MenuItem(R.drawable.grid_dotask,
								Config.GRID_REPAIR_TASK, Config.FUNCTION_REPAIRTASK));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_REPAIRDISTRIBUTE)) {
						menus.add(new MenuItem(R.drawable.grid_distribution,
								Config.GRID_DISTRIBUTION, Config.FUNCTION_REPAIRDISTRIBUTE));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_RESSIGNMENT)) {
						menus.add(new MenuItem(R.drawable.grid_change_distribution,
								Config.GRID_CHANGE_DISTRIBUTION,
								Config.FUNCTION_RESSIGNMENT));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_MANAGERDISPATCH)) {
						menus.add(new MenuItem(R.drawable.grid_distribution,
								Config.GRID_MANAGER_DISPATCH,
								Config.FUNCTION_MANAGERDISPATCH));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_MANAGERREDISPATCH)) {
						menus.add(new MenuItem(R.drawable.grid_change_distribution,
								Config.GRID_MANAGER_REDISPATCH,
								Config.FUNCTION_MANAGERREDISPATCH));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_OUTTIMEREPAIR)) {
						menus.add(new MenuItem(R.drawable.grid_outtimerepair,
								Config.GRID_OUTTIME_REPAIR, Config.FUNCTION_OUTTIMEREPAIR));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_RETURNVISIT)) {
						menus.add(new MenuItem(R.drawable.grid_phone_call,
								Config.GRID_PHONECALL, Config.FUNCTION_RETURNVISIT));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_HISTORYQUERY)) {
						menus.add(new MenuItem(R.drawable.grid_histories,
								Config.GRID_HISTOTY_SEARCH, Config.FUNCTION_HISTORYQUERY));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_GET_WORKER_HISTORYLIST)) {
						menus.add(new MenuItem(R.drawable.grid_myhistory,
								Config.GRID_REPORTER_HISTOTY_SEARCH,
								Config.FUNCTION_GET_WORKER_HISTORYLIST));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_PMREVISIT)) {
						menus.add(new MenuItem(R.drawable.grid_stop_phone_call,
								Config.GRID_PMREVISIT, Config.FUNCTION_PMREVISIT));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_OUTDOORREPAIR)) {
						menus.add(new MenuItem(R.drawable.grid_public_apply,
								Config.GRID_PUBLIC_APPLY, Config.FUNCTION_OUTDOORREPAIR));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_MYAPPLY)) {
						menus.add(new MenuItem(R.drawable.grid_myapply,
								Config.GRID_MY_APPLY, Config.FUNCTION_MYAPPLY));
					}
					if (functionCodeListOrder.get(i).equals(Config.FUNCTION_OUTTIMEREPAIROFDEPART)) {
						menus.add(new MenuItem(R.drawable.grid_outtimerepair,
								Config.GRID_OUTTIME_REPAIR, Config.FUNCTION_OUTTIMEREPAIROFDEPART));
					}
			}
			if (true) {// 更多
				menus.add(new MenuItem(R.drawable.grid_more, Config.GRID_MORE, ""));
			}
		}
		
	}
	
	private void intentActivity(String functionname) {
		if (functionname.equals(Config.FUNCTION_REPAIRALLY)) {// 户内报修
			intent = new Intent(context, WritePaperActivity.class);
		}
		if (functionname.equals(Config.FUNCTION_OUTDOORREPAIR)) {// 公区报修
			intent = new Intent(context, WriteOutdoorPaperActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_OUTDOORAPPLYREPAIR);
		}
		if (functionname.equals(Config.FUNCTION_REPAIRPROGRESS)) {// 维修进度
			bundle.putString(Config.FUNCTIONCODE,
					Config.FUNCTION_REPAIRPROGRESS);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_REPAIRPROGRESS);
		}
		if (functionname.equals(Config.FUNCTION_HOUSEMANAGE)) {// 房屋管理
			intent = new Intent(context, HouseManageListActivity.class);
		}
		if (functionname.equals(Config.FUNCTION_REPAIRTASK)) {// 维修任务
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_REPAIRTASK);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_REPAIRTASK);
		}
		if (functionname.equals(Config.FUNCTION_REPAIRDISTRIBUTE)) {// 派单
			bundle.putString(Config.FUNCTIONCODE,
					Config.FUNCTION_REPAIRDISTRIBUTE);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_DISPATCH);
		}
		if (functionname.equals(Config.FUNCTION_RETURNVISIT)) {// 回访
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_RETURNVISIT);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_REVISIT);
		}
		if (functionname.equals(Config.FUNCTION_RESSIGNMENT)) {// 改派
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_RESSIGNMENT);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_REDISPATCH);
		}
		if (functionname.equals(Config.FUNCTION_OUTTIMEREPAIR)) {// 超时派单(派单员)
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_OUTTIMEREPAIR);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_OVERTIMEREDISPATCH);
		}
		if (functionname.equals(Config.FUNCTION_OUTTIMEREPAIROFDEPART)) {// 超时派单（部门派单员）
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_OUTTIMEREPAIROFDEPART);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_OVERTIMEREDISPATCH);
		}
		if (functionname.equals(Config.FUNCTION_HISTORYQUERY)) {// 历史查询
			intent = new Intent(context, HistorySearchActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_HISTORY);
		}
		if (functionname.equals(Config.FUNCTION_GET_WORKER_HISTORYLIST)) {// 自己提单历史查询
			intent = new Intent(context, HistorySearchActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_REPORTE_HISTORY);
		}
		if (functionname.equals(Config.FUNCTION_MYAPPLY)) {// 我的提单
			Toast.makeText(context, "我的提单", Toast.LENGTH_SHORT).show();
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_MYAPPLY);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_MYAPPLY);
		}
		if (functionname.equals(Config.FUNCTION_MANAGERDISPATCH)) {// 部门主管派单
			bundle.putString(Config.FUNCTIONCODE,
					Config.FUNCTION_MANAGERDISPATCH);
			intent = new Intent(context, ToDoTaskListActivity.class);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_DEPART_DISPATCHER_DISPATCH);
		}
		if (functionname.equals(Config.FUNCTION_MANAGERREDISPATCH)) {// 部门主管改派
			bundle.putString(Config.FUNCTIONCODE,
					Config.FUNCTION_MANAGERREDISPATCH);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_DEPART_DISPATCHER_REDISPATCH);
			intent = new Intent(context, ToDoTaskListActivity.class);
		}
		if (functionname.equals(Config.FUNCTION_PMREVISIT)) {// 项目经理终止回访
			bundle.putString(Config.FUNCTIONCODE, Config.FUNCTION_PMREVISIT);
			bundle.putString(Config.FUNCTIONTITLENAME,
					Config.FUNCTIONTITLENAME_STOPREVISIT);
			intent = new Intent(context, ToDoTaskListActivity.class);

		}
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
	}

	// 当指定Tab被选中时激发该方法
	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
		mpager.setCurrentItem(tab.getPosition());  //②
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
	}
}
